package at.donalfonso.ui.group;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import at.donalfonso.model.Gruppe;
import at.donalfonso.model.LieferKunde;
import at.donalfonso.model.Pizza;
import at.donalfonso.model.Pizza.PizzaEnum;

public class GroupView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3122998223942582072L;

	private JPanel groupConfigPanel;

	private JPanel groupQueuePanel = null;
	private JPanel lieferPanel = null;
	private JScrollPane groupSystemPanel = null;
	private JTextField address = null;
	private JCheckBox lieferBox = null;
	private JCheckBox addressExist = null;
	private JComboBox<Integer> filialID = null;

	private JComboBox<Pizza.PizzaEnum> pizza1;
	private JComboBox<Pizza.PizzaEnum> pizza2;
	private JComboBox<Pizza.PizzaEnum> pizza3;
	private JComboBox<Pizza.PizzaEnum> pizza4;
	
	private JButton buttonSpace;
//	private JButton buttonTest;
	private ActionListener listener;

	private DefaultListModel<Gruppe> groupQueueModel = null;
	private DefaultListModel<Gruppe> groupSystemModel = null;
	

	public GroupView(ActionListener listener) throws HeadlessException {
		
		setSize(660, 800);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.listener = listener;
		
		initialize();
		setVisible(true);

	}

	
	/**
	 * Initialisieren des Gruppen-GUIs.
	 */
	private void initialize() {
		JPanel headerPanel = new JPanel();

		headerPanel.setLayout(new BorderLayout());
		setLayout(new BorderLayout());

		headerPanel.add(getGroupConfigPanel(), BorderLayout.NORTH);
		headerPanel.add(getGroupQueuePanel(), BorderLayout.CENTER);

		getContentPane().add(headerPanel, BorderLayout.NORTH);
		getContentPane().add(getGroupSystemPanel(), BorderLayout.CENTER);

		getGroupQueuePanel().setPreferredSize(new Dimension(MAXIMIZED_HORIZ, 200));

	}

	private JPanel getGroupConfigPanel(){

		if (groupConfigPanel == null){
			groupConfigPanel = new JPanel();
			groupConfigPanel.setBorder(BorderFactory.createTitledBorder("Configure Group"));

			groupConfigPanel.setLayout(new BorderLayout());

			JPanel pizzaConfigPanel = new JPanel();
			JPanel buttonPanel = new JPanel();

			pizza1 = new JComboBox<Pizza.PizzaEnum>(Pizza.PizzaEnum.values());
			pizza2 = new JComboBox<Pizza.PizzaEnum>(Pizza.PizzaEnum.values());
			pizza3 = new JComboBox<Pizza.PizzaEnum>(Pizza.PizzaEnum.values());
			pizza4 = new JComboBox<Pizza.PizzaEnum>(Pizza.PizzaEnum.values());

			pizza1.insertItemAt(null, 0);
			pizza2.insertItemAt(null, 0);
			pizza3.insertItemAt(null, 0);
			pizza4.insertItemAt(null, 0);

			pizza1.setSelectedIndex(0);
			pizza2.setSelectedIndex(0);
			pizza3.setSelectedIndex(0);
			pizza4.setSelectedIndex(0);

			filialID = new JComboBox<Integer>(new Integer []{1,2});
			
			pizzaConfigPanel.add(new JLabel("FilialID: "));
			pizzaConfigPanel.add(filialID);
			
			pizzaConfigPanel.add(pizza1);
			pizzaConfigPanel.add(pizza2);
			pizzaConfigPanel.add(pizza3);
			pizzaConfigPanel.add(pizza4);

			buttonPanel.add(new JButton(new AbstractAction("Add to Queue") {

				public void actionPerformed(ActionEvent arg0) {
					addToQueue();
				}
			}));
			

			
			groupConfigPanel.add(pizzaConfigPanel, BorderLayout.NORTH);
			groupConfigPanel.add(getLieferpanel(), BorderLayout.CENTER);
			groupConfigPanel.add(buttonPanel, BorderLayout.SOUTH);
		}

		return groupConfigPanel;
	}
	
	private JPanel getLieferpanel(){
		
		if (lieferPanel == null){
			lieferPanel = new JPanel();  
			lieferPanel.setLayout(new BorderLayout());
			lieferPanel.setBorder(BorderFactory.createTitledBorder("Lieferbestellung"));
			
			lieferBox = new JCheckBox();
			JLabel label = new JLabel("Bestellung ist eine Lieferbestellung");
			JPanel panel = new JPanel();
			panel.add(lieferBox);
			panel.add(label);
			lieferPanel.add(panel, BorderLayout.NORTH);
			
			label = new JLabel("Die Adresse existiert.");
			addressExist = new JCheckBox();
			panel = new JPanel();
			panel.add(addressExist);
			panel.add(label);
			
			address = new JTextField("addr1");
						
			
			lieferPanel.add(panel, BorderLayout.CENTER);
			lieferPanel.add(address, BorderLayout.SOUTH);
		}
		return lieferPanel;
	}

	private JPanel getGroupQueuePanel(){

		if (groupQueuePanel == null){
			groupQueuePanel = new JPanel();
			groupQueuePanel.setLayout(new BorderLayout());

			JList<Gruppe> listen = new JList<Gruppe>();
			listen.setModel(getGroupQueueModel());

			JScrollPane scrollPanel = new JScrollPane(listen);

			groupQueuePanel.add(scrollPanel, BorderLayout.CENTER);

			JPanel buttonPanel = new JPanel();

			buttonSpace = new JButton("Send queue to Space");
			buttonSpace.addActionListener(listener);
//			buttonTest = new JButton("Testdaten schicken");
//			buttonTest.addActionListener(listener);
			buttonPanel.add(buttonSpace);
//			buttonPanel.add(buttonTest);
			groupQueuePanel.add(buttonPanel, BorderLayout.SOUTH);
			groupQueuePanel.setBorder(BorderFactory.createTitledBorder("Groups in Queue"));
		}
		return groupQueuePanel;
	}

	private JScrollPane getGroupSystemPanel(){

		if (groupSystemPanel == null){

			JList<Gruppe> listen = new JList<Gruppe>();
			listen.setModel(getGroupSystemModel());

			groupSystemPanel = new JScrollPane(listen);
			groupSystemPanel.setBorder(BorderFactory.createTitledBorder("Groups in Space"));
		}
		return groupSystemPanel;
	}

	/**
	 * Fügt die bestellten Pizzen zu einer Gruppe zusammen und hängt diese in 
	 * die Queue.
	 */
	public void addToQueue(){

		if (pizza1.getSelectedItem() != null ||
			pizza2.getSelectedItem() != null ||
			pizza3.getSelectedItem() != null ||
			pizza4.getSelectedItem() != null){

			System.out.println("addToQueue: "+lieferBox.isSelected());
			int id = (int) filialID.getSelectedItem();

			PizzaEnum[] pizzawunsch = {(Pizza.PizzaEnum) pizza1.getSelectedItem(), (Pizza.PizzaEnum) pizza2.getSelectedItem(), (Pizza.PizzaEnum) pizza3.getSelectedItem(), (Pizza.PizzaEnum) pizza4.getSelectedItem()};
			
			if (lieferBox.isSelected()) {
				// Die Bestellung ist eine Lieferbestellung.
				LieferKunde kunde = new LieferKunde(id, address.getText(), addressExist.isSelected(), pizzawunsch[0], pizzawunsch[1], pizzawunsch[2], pizzawunsch[3]);
				getGroupQueueModel().addElement(kunde);
				
			}else{
				Gruppe group = new Gruppe(id, pizzawunsch[0], pizzawunsch[1], pizzawunsch[2], pizzawunsch[3]);
				getGroupQueueModel().addElement(group);
			}
		}	

	}

	public DefaultListModel<Gruppe> getGroupQueueModel(){
		if (groupQueueModel == null){
			groupQueueModel = new DefaultListModel<Gruppe>();
		}
		return groupQueueModel;
	}
	
	public DefaultListModel<Gruppe> getGroupSystemModel(){
		if (groupSystemModel == null){
			groupSystemModel = new DefaultListModel<Gruppe>();
		}
		return groupSystemModel;
	}
	
}


